package com.rfid.emergencywarehouse.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.base.util.AnimatorHelper;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.MutilChoiceScanBarCodeAdapter;
import com.rfid.emergencywarehouse.adapter.ReturnAssetAdapter;
import com.rfid.emergencywarehouse.adapter.ScanBarcodeAdapter;
import com.rfid.emergencywarehouse.adapter.SelectAdapter;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.entity.NameValueBean;
import com.rfid.emergencywarehouse.manager.FragmentDataManager;
import com.rfid.emergencywarehouse.presenter.ReturnAssetPresenter;
import com.rfid.emergencywarehouse.view.IReturnView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReturnFragment extends BaseScanListFragment<ReturnAssetAdapter, BarcodeInfo, ReturnAssetPresenter>implements IReturnView,FragmentDataManager.Listener {

    int mClickId = 0;
    List<NameValueBean> mMaterialStatusList;
    NameValueBean mMaterailStatus;

    @BindView(R.id.material_status_name)
    TextView mMaterialStatusName;

    @BindView(R.id.barcode)
    EditText mBarcodeEdit;

    @Override
    protected int getScanRfidCheckBoxId() {
        return R.id.scan;
    }

    @Override
    protected EditText getBarcodeEdit() {
        return mBarcodeEdit;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter.getWGStatusConfigs();
        setTitle("物资归还");
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        FragmentDataManager.INSTANCE.addListener(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        FragmentDataManager.INSTANCE.removeListener(this);
    }

//    @Override
//    protected String getScanTip(int size) {
//        return String.format("已扫描数据%s条，请选择你要归还的物资",size);
//    }

    @Override
    protected ReturnAssetAdapter getAdapter() {
        return new ReturnAssetAdapter(mData);
    }

//    @Override
//    public void onSelect(BarcodeInfo info, boolean select) {
//        if (select) {
//            if (!mSelects.contains(info)) {
//                mSelects.add(info);
//            }
//        } else {
//            mSelects.remove(info);
//        }
//        if (mSelects.size() > 0) {
//            mBarcodeEdit.setText("");
//            mBarcodeEdit.setEnabled(false);
//        } else {
//            mBarcodeEdit.setEnabled(true);
//        }
//    }

    @Override
    public ReturnAssetPresenter getPresenter() {
        return new ReturnAssetPresenter();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_return_asset;
    }

    @Override
    public void onGetWGStatusConfigs(List<NameValueBean> beans) {
        mMaterialStatusList = beans;
        FragmentDataManager.INSTANCE.setData(beans);
    }

    @Override
    public void onReturnResult(boolean success, String msg) {
        Context context = getContext();
        if (context == null) {
            return;
        }
        if (success) {
            showSuccess("归还物资成功");
            clearSelectData();
            mBarcodeEdit.setText("");
        } else {
            showTip(msg);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        showSelectInfo();
    }

    @OnClick({R.id.materialStatus})
    public void click(View v) {
        int id = v.getId();
        System.out.println("click id = " + id);
        mClickId = id;
        FragmentDataManager.INSTANCE.select(null);
        FragmentDataManager.INSTANCE.setData(null);
        if (id == R.id.materialStatus) {
            if (mMaterialStatusList == null) {
                mPresenter.getWGStatusConfigs();
            } else {
                FragmentDataManager.INSTANCE.setData(mMaterialStatusList);
            }
        }

        NavHostFragment.findNavController(this).navigate(R.id.action_returnFragment_to_selectFragment2);
    }

    @Override
    public void onSelect(NameValueBean v) {
        System.out.println("onSelect = " + v);
        FragmentDataManager.INSTANCE.select(null);
        if (mClickId == R.id.materialStatus) {
            mMaterailStatus = v;
        }
        showSelectInfo();
    }

    private void showSelectInfo() {
        System.out.println(mMaterailStatus != null);
        if (mMaterailStatus != null) {
            mMaterialStatusName.setText(mMaterailStatus.getName());
        }
    }

    @OnClick(R.id.right_btn)
    public void returnAsset(View v) {
        System.out.println("returnAsset mMaterailStatus = "  +mMaterailStatus);
//        if (mMaterailStatus == null) {
//            AnimatorHelper.shake(getContext(),mMaterialStatusName);
//            return;
//        }
//        String barcode = mBarcodeEdit.getText().toString();
//        if (TextUtils.isEmpty(barcode) && mSelects.size() == 0) {
//            AnimatorHelper.shake(getContext(),mBarcodeEdit);
//            return;
//        }
        List<String> barcodes = getBarcodes();
        if (barcodes == null) {
            return;
        }
        mPresenter.returnAsset(mSelects);
    }

    @Override
    protected void onStartScan() {
        mSelects.clear();
    }

    @Override
    protected boolean needAdd(BarcodeInfo info) {
        return !info.isNew();
    }

    @Override
    public void onEdit(BarcodeInfo info) {
        showPopWindow(info);
    }


    private void showPopWindow(BarcodeInfo info) {
        List<NameValueBean> dangAnTags = mMaterialStatusList;
        if (dangAnTags == null) {
            return;
        }
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.popup_shelf_layout, null);
        PopupWindow pop =new PopupWindow(view, metrics.widthPixels, metrics.heightPixels, false);
        pop.showAtLocation(mRoot, Gravity.LEFT,0,0);
        View quit = view.findViewById(R.id.back);
        View confirm = view.findViewById(R.id.confirm);
        RecyclerView listView = view.findViewById(R.id.list);
        listView.setLayoutManager(new LinearLayoutManager(getContext()));
        SelectAdapter adapter = new SelectAdapter(dangAnTags);
        listView.setAdapter(adapter);
        RadioGroup rg = view.findViewById(R.id.rg);
        if (info.isEnable()) {
            rg.check(R.id.yes);
        } else {
            rg.check(R.id.no);
        }
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = rg.getCheckedRadioButtonId();
                boolean enable = (id == R.id.yes)?true:false;
                NameValueBean bean = adapter.getSelected();
                info.setShapeStatus(bean.getName());
                info.setEnable(enable);
                mAdapter.notifyDataSetChanged();
                pop.dismiss();
            }
        });
    }
}
